home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 17 / program8.c < prev    next >
Text File  |  1989-08-23  |  7KB  |  266 lines

  1. /*  PROG8OUT.C -- Produces the same result as PROGRAM6.C, by reading data from
  2.     a data file created with the program PROG8IN.C.  */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define MAX_ROWS 99
  8. #define MAX_NAME 100
  9.  
  10. /*  Define the heading information for the output file.  */
  11.  
  12. #define HEAD1 "John Smith"
  13. #define HEAD2 "CIS 176, Section 02A"
  14. #define HEAD3 "Program 8"
  15.  
  16. /*  Create a structure template for the structure with the tag "record".  */
  17.  
  18. struct record {
  19.         char name[30];
  20.         int cover,
  21.             code;
  22.           };
  23.  
  24. FILE *fptr;
  25. FILE *out;
  26.  
  27. main()
  28. {
  29.     struct record inputt;
  30.     char names[MAX_ROWS][MAX_NAME];   /*  Employee Names.  */
  31.     char coverage[MAX_ROWS][20];      /*  Coverage types.  */
  32.     char plan[MAX_ROWS][20];          /*  Health Plan names.  */
  33.     float costs[MAX_ROWS][2];         /*  Employee and Employer costs.  */
  34.     int count, cx;                        /*  Counter variable.  */
  35.     float empe_total, empr_total;     /*  Variables to hold total costs.  */
  36.  
  37.     /*  Array of costs for the different Health Plans and types of
  38.         coverage.  */
  39.  
  40.     float hp_costs[3][4] = {{25.00,30.00,40.00,45.00},
  41.                 {30.00,40.00,55.00,60.00},
  42.                 {20.00,25.00,40.00,70.00}};
  43.  
  44.     count = 0;
  45.     empe_total = 0;
  46.     empr_total = 0;
  47.  
  48.     /*  Open data file.  */
  49.  
  50.     if((fptr=fopen("indata","rb")) == NULL)
  51.     {
  52.         printf("\nCannot open file for storing data.");
  53.         exit(1);
  54.     }
  55.  
  56.     /*  Read in employee information from data file.  */
  57.  
  58.     while(fread(&inputt,sizeof(inputt),1,fptr)==1)
  59.     {
  60.         printf("\nName:  %s", inputt.name);
  61.         strcpy(names[count],inputt.name);
  62.  
  63.         printf("\nCoverage Code:  %d", inputt.cover);
  64.         get_cover(inputt.cover, coverage, count);
  65.  
  66.         printf("\nHealth Plan Code:  %d\n", inputt.code);
  67.         get_plan(inputt.code, plan, count);
  68.  
  69.         calc_costs(inputt.cover, inputt.code, costs, hp_costs);
  70.  
  71.         count++;
  72.     }
  73.  
  74.     /*  Close data file.  */
  75.  
  76.     fclose(fptr);
  77.  
  78.     /*  Calculate the total cost, for the employer and employees, of
  79.         the health plans.  */
  80.  
  81.     total_costs(costs, &empe_total, &empr_total, count);
  82.  
  83.     /*  Write data to output file.  */
  84.  
  85.     print_data(names, coverage, plan, costs, empe_total, empr_total,
  86.          count, out);
  87.  
  88. }
  89.  
  90. /*  Get the type of health care coverage corresponding to the employee's
  91.     health care code.  */
  92.  
  93. get_cover(cover, array, count)
  94. int cover, count;
  95. char array[][20];
  96. {
  97.     /*  Initialize/declare an array containing the names of each type
  98.         of coverage.  */
  99.  
  100.     int counter = count;
  101.     char typcvr[4][18] = {{"Employee only"},
  102.                   {"Family coverage"},
  103.                   {"Reduced benefits"},
  104.                   {"** Error **"}};
  105.     int temp;
  106.  
  107.     if(cover >2)
  108.     {
  109.         /*  If the code for the type of coverage is greater
  110.         than 2, assign "** Error **" to that code.  */
  111.  
  112.         temp = 3;
  113.         strcy(array[counter],typcvr[temp]);
  114.     }
  115.     else
  116.         strcy(array[counter],typcvr[cover]);
  117.  
  118. }
  119.  
  120. /*  Get the name of the Health plan, given the Health Plan Code.  */
  121.  
  122. get_plan(code, array, count)
  123. int code, count;
  124. char array[][20];
  125. {
  126.     /*  Using a switch statement, determine the health plan name
  127.         for the code just entered.  If the code entered is not
  128.         a valid code, assign "** Error **" to that code.  */
  129.  
  130.     switch(code)
  131.     {
  132.         case 0:
  133.             strcy(array[count],"Standard");
  134.             break;
  135.         case 1:
  136.             strcy(array[count],"HMO");
  137.             break;
  138.         case 2:
  139.             strcy(array[count],"Premium");
  140.             break;
  141.         case 3:
  142.             strcy(array[count],"ACME");
  143.             break;
  144.         default:
  145.             strcy(array[count],"** Error **");
  146.             break;
  147.     }
  148. }
  149.  
  150. /*  Calculate the health care costs for the employee and the employer.  */
  151.  
  152. calc_costs(cover, code, array3, array4)
  153. int cover, code;
  154. float array3[][2], array4[][4];
  155. {
  156.     float tempcost[MAX_ROWS];
  157.     int count;
  158.  
  159.     /*  If either the coverage code or health plan code is invalid,
  160.         set the health care cost to 0; otherwise, determine the cost
  161.         from the table in main().  */
  162.  
  163.     if(cover > 2 || code > 3)
  164.         tempcost[count] = 0.00;
  165.     else
  166.         tempcost[count] = array4[cover][code];
  167.  
  168.     /*  For the cost of the health plan, determine the amount paid by the
  169.         employer and the employee.  (The employer pays 95% of the total
  170.         cost and the employee pays the remaining 5% of the total cost.)  */
  171.  
  172.     array3[count][1] = ((tempcost[count]) * (0.05));
  173.  
  174.     array3[count][2] = ((tempcost[count]) * (0.95));
  175.  
  176. }
  177.  
  178. /*  Calculate the total costs paid by the employees and the employer.  */
  179.  
  180. total_costs(array, total1, total2, size)
  181. float array[][2], *total1, *total2;
  182. int size;
  183. {
  184.     int counter;
  185.  
  186.     for(counter = 0; counter < size; counter++)
  187.         *total1 += array[counter][1];
  188.  
  189.     for(counter = 0; counter < size; counter++)
  190.         *total2 += array[counter][2];
  191. }
  192.  
  193. /*  Print the health plan data to a file.  Each entry will consist of the
  194.     employee's name, the type of health insurance coverage, the name of the
  195.     health plan, the amount paid by the employee, and the amount paid by the
  196.     employer.  At the end of the file, there will be totals given for the
  197.     amounts paid by the employees and the employer.  */
  198.  
  199. print_data(array1, array2, array3, array4, total1, total2, size, out)
  200. char array1[][MAX_NAME+1], array2[][20], array3[][20];
  201. float array4[][2], total1, total2;
  202. int size;
  203. FILE *out;
  204. {
  205.     int counter;
  206.  
  207.     /*  Open the file to contain the results.  */
  208.  
  209.     out = fopen("d:output8", "w");
  210.  
  211.     /*  Print the header and column headings to the output file.  */
  212.  
  213.     header(out);
  214.     heading(out);
  215.  
  216.     for(counter = 0; counter < size; counter++)
  217.     {
  218.         printf("\n%s", array1[counter]);
  219.         fprintf(out, "\n %-15s   %-19s   %-12s  $%6.2f     $%6.2f",
  220.              array1[counter],array2[counter], array3[counter],array4[counter][1],array4[counter][2]);
  221.     }
  222.     /*  Print the totals, seperated by a line of hyphens, for the employer
  223.         and the employees.  */
  224.  
  225.     fprintf(out, "\n%54c----------  ----------", ' ');
  226.     fprintf(out, "\n Totals:%46c$%7.2f    $%7.2f\n\n", ' ', total1, total2);
  227.  
  228.     /*  Close the output file.  */
  229.  
  230.     fclose(out);
  231. }
  232.  
  233. /*  Print the headings for the columns in the output file.  */
  234.  
  235. heading(out)
  236. FILE *out;
  237. {
  238.     fprintf(out, "\n%3cEmployee%30cHealth%9cEmployee%3cEmployer", ' ',
  239.           ' ', ' ', ' ');
  240.     fprintf(out, "\n%4cName%11cType of Coverage%7cPlan%12cCost%6cCost\n",
  241.           ' ', ' ', ' ', ' ', ' ');
  242.  
  243. }
  244.  
  245. /*  Copy the contents of one string (original) over the contents of another
  246.     string (copy).  */
  247.  
  248. strcy(copy, original)
  249. char *copy, *original;
  250. {
  251.     while(*copy++ = *original++);
  252. }
  253.  
  254. /*  Print the header information to the output file.  */
  255.  
  256. header(out)
  257. FILE *out;
  258. {
  259.      fprintf(out,"%30c%s\n",' ', HEAD1);
  260.      fprintf(out,"%29c%s\n",' ', HEAD2);
  261.      fprintf(out,"%34c%s\n",' ', HEAD3);
  262. }
  263.  
  264.  
  265.  
  266.